//
//  ViewController.m
//  UI_Homework_07_03
//
//  Created by 黄威 on 16/5/11.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ViewController.h"
#import "UIimagepicker.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property(nonatomic,retain) UIImageView * imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    [self create];
    
    
    
}

- (void)create{

    self.view.backgroundColor = [UIColor cyanColor];
    
    self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"6.jpg"]];
    
    self.imageView.frame = CGRectMake(0, 0, 200, 400);
    self.imageView.center = self.view.center;
    [self.view addSubview:self.imageView];

    self.navigationItem.title = @"相片选择器";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(handle)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(handle2)];
   
    [_imageView release];
    

}

- (void)handle{
   
    UIImagePickerController *pick = [[UIImagePickerController alloc] init];
    
    pick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pick.allowsEditing = YES;
 
    pick.delegate = self;
    
    [self presentViewController:pick animated:YES completion:^{
        
    }];
    
   
  
    }


- (void)handle2{

    {
        //创建图片选择器
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        
        //指定源类型前，检查图片源是否可用
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            //指定源的类型
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            //在选定图片之前，用户可以简单编辑要选的图片。包括上下移动改变图片的选取范围，用手捏合动作改变图片的大小等。
            imagePicker.allowsEditing = YES;
            
            //实现委托，委托必须实现UIImagePickerControllerDelegate协议，来对用户在图片选取器中的动作
            imagePicker.delegate = self;
            
            //设置完iamgePicker后，就可以启动了。用以下方法将图像选取器的视图“推”出来
            [self presentModalViewController:imagePicker animated:YES];
            
            [imagePicker release];
        }
        else
        {
            UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"相机不能用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }
 
}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
//    UIImageView *vie = [self.view viewWithTag:1000];
//    vie.image = [info objectForKey:UIImagePickerControllerOriginalImage];
//    
//    UIImageView *vie1 = [self.view viewWithTag:100];
//    vie1.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    
    self.imageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
